package com.example.tianling.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.params.AiResultDo;
import com.example.tianling.common.utils.R;
import com.example.tianling.common.vo.AiResultListVo;
import com.example.tianling.common.vo.AiResultVo;
import com.example.tianling.entity.AiResultEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.AiResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AiResultController {
    @Autowired
    private AiResultService aiResultService;

    @Login
    @PostMapping("/ai_result/save")
    @ResponseBody
    public R saveResult(@RequestBody AiResultDo aiResultDo, @LoginUser UserEntity user) throws Exception {
        System.out.println(aiResultDo);
        aiResultService.saveResult(user.getUserId(), aiResultDo);
        return R.success();
    }

    @Login
    @PostMapping("/ai_result/list")
    @ResponseBody
    public R getAiResultList(@LoginUser UserEntity user) {
        List<AiResultListVo> aiResultList = aiResultService.getAiResultList(user.getUserId());
        System.out.println(aiResultList);
        return R.success(aiResultList);
    }

    @Login
    @PostMapping("/ai_result/detail")
    @ResponseBody
    public R getAiResult(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        Integer id = paramsJSONObject.getInteger("id");
        AiResultVo aiResult = aiResultService.getAiResult(id);
        return R.success(aiResult);
    }

    @Login
    @PostMapping("/ai_result/update_note")
    @ResponseBody
    public R updateNote(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        Integer id = paramsJSONObject.getInteger("id");
        String note = paramsJSONObject.getString("note");

        aiResultService.updateNote(id, note);

        return R.success();
    }
}
